﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using FlexnetConsultants.CookieNotification.Settings;
using FlexnetConsultants.CookieNotification.Code;
using Microsoft.SharePoint.Utilities;

namespace FlexnetConsultants.CookieNotification.ControlTemplates.FlexnetConsultants.CookieNotification
{
    public partial class CookieNotificationUC : UserControl
    {

        #region  Settings Properties
        
        public bool CookieMessageEnabled {
            get{
                return CookieSettings != null ? CookieSettings.Enabled : false;                                
            }
        }


        public string CookieName {
            get  {
                return CookieSettings != null ? CookieSettings.CookieName : string.Empty;
            }
        }

        public string CookieMessageTitle  {
            get {
                return CookieSettings != null ? CookieSettings.CookieMessageTitle : string.Empty;               
            }
        }

        public string CookieMessage       {
            get {
                return CookieSettings != null ? CookieSettings.CookieMessageBody : string.Empty;               
            }
        }

        public string AcceptCookieLinkTitle {
            get {
                return CookieSettings != null ? CookieSettings.AcceptCookieLinkTitle : string.Empty;               
            }
        }

        public bool CookieDebugMode {
            get  {
                return CookieSettings != null ? CookieSettings.DebugMode : false;               
            }
        }


        public FCCookieNotificationSettings CookieSettings
        {
            get
            {
                try
                {
                    return FCCookieNotificationSettings.Local;
                }
                catch { return null; }
            }
        }

        public bool MessageControlEnabled
        {
            get;

            set;
        }
     
        public string IconUrl {
          get
            {
                return SPUtility.ReplaceEmbeddedPrefixedUrlsWithServerRelativeUrls( "~site/_layouts/images/FlexnetConsultants.CookieNotification/CookieNotificationInfo.png");               
            }
        }
        #endregion

        #region Page Events

        /// <summary>
        /// Check if we are enabled, are in a dialog or in debug mode
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            bool bIsEnabled = CookieMessageEnabled;
            bool bIsDlg = false;

            try{
                                
                string CookieValue =string.Empty;

                if (Request.Cookies[CookieName] != null)
                    CookieValue = Convert.ToString(Request.Cookies[CookieName].Value);

                if (CookieValue.Equals("true", 
                                StringComparison.InvariantCultureIgnoreCase))
                {
                    System.Diagnostics.Debug.WriteLine("Cookie Agreement has been set.");
                    bIsEnabled = false;
                }

                if (Request["IsDlg"] != null && 
                    Convert.ToString(Request["IsDlg"])=="1")
                {
                    System.Diagnostics.Debug.WriteLine("IsDlg Found on Url");
                    bIsDlg = true;                   
                }
            }
            catch(Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("Exception:" + ex.Message);
                Logger.Current.LogException("CookieNotification", ex);
            }

            // Override for debug mode
            if (CookieDebugMode)
            {
                System.Diagnostics.Debug.WriteLine("CookieDebugMode Override On");
                bIsEnabled = true;
            }

            // Dont show in Dialogs
            if (bIsDlg)
            {
                System.Diagnostics.Debug.WriteLine("Switching off for Dialogs");
                bIsEnabled = false;
            }

            if (!bIsEnabled)
            {
                System.Diagnostics.Debug.WriteLine("Hiding Control");

                phMain.Visible = false;
                slFCCookieCheck.Visible = false;
                slFCCookieCheck.Name = "";

                MessageControlEnabled = false;
            }
            else
            {
                MessageControlEnabled = true;
            }
        }
#endregion
    }
}
